/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sinaEx;

import com.mysql.jdbc.Statement;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.ArrayList;
import java.util.List;
import weibo4j.Timeline;
import weibo4j.Users;
import weibo4j.Weibo;
import weibo4j.model.Paging;
import weibo4j.model.Status;
import weibo4j.model.User;
import weibo4j.model.WeiboException;

/**
 *
 * @author Administrator
 */
public class complement2 {

    public static List<String> id = new ArrayList<String>();

    public static void main(String[] args) throws IOException, WeiboException {
        readfile();
        prepareWeibo();
        int i = 1;
        for (String uid : id) {
            
            System.out.println(i++);
            String statusCount = getStatus(uid);
            if(statusCount.equals("0"))
            System.out.println(uid + " " + statusCount);
            String query = "update `weibonews`.`wb_ex_blogrepost` set `create`='" + statusCount + "' where `uid`='" + uid + "';";
            try {
                Class.forName("com.mysql.jdbc.Driver").newInstance();
                Connection conn = (Connection) DriverManager.getConnection("jdbc:mysql://192.168.8.6:3306/weibonews?useUnicode=true&characterEncoding=gb2312", "root", "root");
                Statement stmt = (Statement) conn.createStatement();
                int a = stmt.executeUpdate(query);
                conn.close();
            } catch (Exception e) {
                e.printStackTrace();
                //break;
            }
        }
    }

    public static String getStatus(String id) throws WeiboException {
        Timeline tm = new Timeline();
        String count = "0";
        List<Status> status = null;
        try {
            status = tm.getUserTimeline(id, "", 200, new Paging(1, 200), 0, 0);
        } catch (Exception e) {

            System.out.println(id + " is time out");
            if (status == null) {
                System.out.println("timeout...");
                int i = 0;
                while (status == null) {
                    try {
                        status = tm.getUserTimeline(id, "", 200, new Paging(1, 200), 0, 0);
                    } catch (Exception ee) {
                    }
                    if (status == null) {
                        System.out.println("timeout...");
                    }
                }
            }
        }
        if (status != null) {
            int c = 0;
            for (Status s : status) {
                if (s.getRetweetedStatus() != null) {
                    User user = s.getRetweetedStatus().getUser();
                    if (user != null) {
                        if (user.getId().equals(id)) {
                            c += 1;
                        }
                    }
                } else {
                    c += 1;
                }
            }
            count = c + "";
        }
        return count;
    }

    public static void prepareWeibo() {
        String accessToken = spider.Auth4Code.getCode();
        Weibo weibo = new Weibo();
        weibo.setToken(accessToken);
    }

    public static void readfile() throws IOException {
        String filename = "src/sinaEx/ex_id_list";
        BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(filename)));
        String line = reader.readLine();
        line = reader.readLine();

        while (line != null) {
            id.add(line);
            line = reader.readLine();
        }
        reader.close();
    }
}
